package com.func.domain.activity.service.quota.rule.impl;

import com.func.domain.activity.model.entity.ActivityCountEntity;
import com.func.domain.activity.model.entity.ActivityEntity;
import com.func.domain.activity.model.entity.ActivitySkuEntity;
import com.func.domain.activity.model.valobj.ActivityStateVO;
import com.func.domain.activity.service.quota.rule.AbstractActionChain;
import com.func.types.enums.ResponseCode;
import com.func.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * <p>
 * ActivityBaseActionChain
 * </p>
 *
 * @author Simon
 * @version 2025.03.12.1.0.0
 * @description 活动的库存, 时间校验
 * @since 2025-03-12
 */

@Slf4j
@Component("activity_base_action")
public class ActivityBaseActionChain extends AbstractActionChain {

    @Override
    public boolean action(ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        log.info("活动责任链-基础信息 : [有效期, 状态, 库存(sku)] 校验开始. sku: {} activityId: {}", activitySkuEntity.getSku(), activitySkuEntity.getActivityId());
        // 校验: 活动状态
        if (!ActivityStateVO.open.getCode().equals(activityEntity.getState())){
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(), ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }

        // 校验: 活动日期
        Date current = new Date();
        if(activityEntity.getBeginDateTime().after(current) || activityEntity.getEndDateTime().before(current)) {
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(), ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        // 校验: 活动sku库存
        if (activitySkuEntity.getStockCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getCode(), ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getInfo());
        }

        log.info("活动责任链-基础信息 : [有效期, 状态, 库存(sku)] 校验结束. sku: {} activityId: {}", activitySkuEntity.getSku(), activitySkuEntity.getActivityId());
        return next().action(activitySkuEntity, activityEntity, activityCountEntity);
    }
}
